import redis


class Redis(object):

    def __init__(self):
        self.conn = redis.Redis(
            # host='127.0.0.1',
            host='39.107.227.235',
            port=6379,
            password='YANhan.1218',
            max_connections=512,
            db=2
        )

    def list_position_url(self, name):
        data = []
        bytes_data = self.conn.lrange(name, 300, 320)
        for item in bytes_data:
            data.append(item.decode())
        return data

    def save_href(self, column_name, data):
        self.conn.lpush(column_name, data)
        return True


if __name__ == '__main__':
    a = Redis()
    data_name = 'java_position_url'
    untreated_urls = a.list_position_url(data_name)
    print('未处理', data_name, '类型', type(untreated_urls))
    print('未处理', data_name, '长度', len(untreated_urls))
    # 去重
    deal_url = list(set(untreated_urls))
    print('已处理', data_name, '类型', type(deal_url))
    print('已处理', data_name, '长度', len(deal_url))
    new_name = 'test_' + data_name
    temp_len = len(deal_url)
    temp_count = 0
    show_process = 0.25
    for item in deal_url:
        temp_count += 1
        a.save_href(new_name, item)
        if temp_count == int(show_process * temp_len - 2):
            print('已完成进度:', show_process * 100, '%')
            show_process += 0.25
    print('添加完毕')
